/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.me.nicefotoappcamp.app;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import org.me.nicefotoappcamp.core.Picture;
import org.me.nicefotoappcamp.provider.Provider;

/**
 *
 * @author Tadas
 */
public class PhotoGallery extends Gallery {

    protected Context mContext;
    protected Provider provider;

    public PhotoGallery(Context context, Provider provider) {
        super(context);
        mContext = context;
        this.provider = provider;
        setAdapter(new ImageAdapter(context));
        setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
        setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView parent, View v, int position, long id) {
                Toast.makeText(mContext, "" + (position + 1), Toast.LENGTH_SHORT).show();
            }
        });
    }

    public Provider getProvider(){
        return this.provider;
    }

    public class ImageAdapter extends BaseAdapter {

        ArrayList<Picture> pictures = new ArrayList<Picture>();
        private Context iCon;

        public ImageAdapter(Context context){
            pictures = getProvider().getPictures();
            iCon = context;
        }

        public int getCount() {
            return pictures.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i = new ImageView(iCon);
            Picture photo = pictures.get(position);
            i.setImageBitmap(BitmapFactory.decodeStream(new ByteArrayInputStream(photo.getPictureBytes())));
            i.setLayoutParams(new Gallery.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
            i.setScaleType(ImageView.ScaleType.FIT_CENTER);
            

            return i;
        }
    }
}
